*****************************
*** GENDER GAP ANALYSIS *****
*** QUINN M. ALBAUGH ********
*** NOVEMBER 2023 VERSION ***
*****************************

*** SET WORKING DIRECTORY
clear all

*** CHANGE SETTINGS FOR GRAPHS
set more off, permanently
grstyle init
grstyle set plain, horizontal nogrid
graph set window fontface "Helvetica"

*** LOAD MULTIPLY IMPUTED DATASET
use "${here}MICE2.dta", clear

keep genderid trans sexualid4 education income born_outside_canada indigenous racialized mother_tongue4 ///
	partyid voteintention votechoice ///
	immigration refugees jobs_vs_environment_pes family_values bilingualism equal_rights immigrant_integration immigrant_jobs abortion conversion_therapy international_trade government_intervention income_inequality wealth_gap ///
	age4 pt user_language cps21_weight_general_all ///
	_mi _mj

mi import ice, automatic clear

*** GENERATE LEFT-RIGHT SCALE
gen left = (immigration + refugees + jobs_vs_environment_pes + family_values + bilingualism + equal_rights + immigrant_integration + immigrant_jobs + abortion + conversion_therapy + government_intervention + income_inequality + wealth_gap) / 13

*** GENERATE VECTOR OF DUMMY VARIABLES FOR PARTY IDENTIFICATION
tab partyid, gen(pid_)

*** GENERATE VECTOR OF DUMMY VARIABLES FOR VOTE CHOICE
tab votechoice, gen(vote_)

*** RECODE INCOME
recode income (1=2)
gen income7 = income - 1
label variable income7 "Income"

*************************************************
*** SENSITIVITY: LIBERAL PARTY IDENTIFICATION ***
*************************************************

/*
forval i = 1/18 {
	forval j = 1/100 {
		set seed `j'
		preserve
		keep if _mi_m == 0
		keep if genderid == 3 & trans == 2
		gen random = runiform()
		sort random
		gen Drop = 1 if _n <= `i'
		keep if Drop == 1
		keep _mi_id Drop
		save DropNBDK_`i'_`j'.dta, replace
		restore
				
		preserve
		merge m:1 _mi_id using DropNBDK_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
				
		mi estimate: logit pid_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using LiberalPartyID_DropNBDK_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}

forval i = 1/18 {
	forval j = 1/100 {
		set seed `j'
				
		preserve
		merge m:1 _mi_id using DropNBDK_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
				
		mi estimate: logit pid_2 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using ConservativePartyID_DropNBDK_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}

forval i = 1/18 {
	forval j = 1/100 {
		set seed `j'
				
		preserve
		merge m:1 _mi_id using DropNBDK_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
				
		mi estimate: logit pid_3 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using NDPPartyID_DropNBDK_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}
*/

forval i = 1/18 {
	forval j = 1/100 {
		set seed `j'
				
		preserve
		merge m:1 _mi_id using DropNBDK_`i'_`j'.dta
		drop if Drop == 1
		drop _merge Drop				
				
		mi estimate: logit vote_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
		mimrgns i.genderid , predict(pr) pwcompare

		tempname memhold
		postfile `memhold' Model Number Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
			using LiberalVote_DropNBDK_`i'_`j'.dta, replace 

			matrix row = r(table_vs)
			local diff1 = row[1,1]
			local diff2 = row[1,2]
			local diff3 = row[1,3]
			local lower1 = row[5,1]
			local lower2 = row[5,2]
			local lower3 = row[5,3]
			local upper1 = row[6,1]
			local upper2 = row[6,2]
			local upper3 = row[6,3]
			local p1 = row[4,1]
			local p2 = row[4,2]
			local p3 = row[4,3]
		post `memhold' (`j') (`i') (`diff1') (`diff2') (`diff3') ///
						(`lower1') (`lower2') (`lower3') ///
						(`upper1') (`upper2') (`upper3') ///
						(`p1') (`p2') (`p3')
		postclose `memhold'
		restore
	}
}




/*
use LiberalPartyID_Baseline.dta, clear
forval i = 1/18 {
	forval j = 1/100 {
		append using LiberalPartyID_DropReplaceNBDK_`i'_`j'.dta
	}
}

gen Sig2 = 0
replace Sig2 = 1 if p2 < 0.1

tab Number Sig2
*** All Significant 

gen Sig3 = 0
replace Sig3 = 1 if p3 < 0.1

tab Number Sig3
*** All Significant

save "LiberalPartyID_DropReplaceNBDK.dta", replace

graph twoway line Sig2 Number, ///
	lcolor(black) ///
	xscale(range(0 30)) ///
	xlabel(0(5)30) ///
	xtitle("Number of Nonbinary" "Respondents Dropped") ///
	yscale(range(0 101) titlegap(2)) ///
	ylabel(0(50)100) ///
	ytitle("Number of Simulations" " "  "(p < 0.1)") ///
	title("Liberal Party Identification" "Men-Nonbinary People", size(medsmall))